package com.atguigu.servlet;

import com.atguigu.bean.User;
import com.atguigu.service.UserService;
import com.atguigu.service.impl.UserServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

/**
 * @Author zhangchunsheng
 * @CreateTime: 2025/01/08
 */
@WebServlet("/UserServlet/*")
public class   UserServlet extends BaseServlet{

//    private UserDao userDao = new UserDaoImpl();

    private UserService userService = new UserServiceImpl();

    /**
    * @Author: zhangchunsheng
    * @Date: 2025/1/8 14:15
     * 登录
    */
    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 获取请求参数(account,password)
        String username = request.getParameter("account");
        String password = request.getParameter("password");

        //2. 调用Dao
        User user = userService.login(new User(null, username, password, null));
        //3. 判断是否登录成功,响应页面
        if(user == null){
            //登录失败(login.html)
            System.out.println("登录失败!");
            response.sendRedirect(request.getContextPath()+"/login.jsp");
        }else{
            //登录成功(index.html)
            //将用户数据共享到域中(session)
            HttpSession session = request.getSession();
            session.setAttribute("user",user);

            System.out.println("登录成功!");
            response.sendRedirect(request.getContextPath()+"/index.jsp");
        }

    }


    /**
     * @Author: zhangchunsheng
     * @Date: 2025/1/8 14:15
     * 注册功能
     */
    protected void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 获取请求参数
        String account = request.getParameter("account");
        String password = request.getParameter("password");
        String nickName = request.getParameter("nickName");

        //2. 调用service
        User user = new User(null,account,password,nickName);
        userService.register(user);

        //3. 响应(注册成功:)
        response.sendRedirect(request.getContextPath()+"/login.jsp");

    }

    /**
     * @Author: zhangchunsheng
     * @Date: 2025/1/8 14:15
     * 退出功能:将用户数据(user)从域中移除,返回login.html页面
     */
    protected void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        //将用户数据(user)从域中移除
        session.removeAttribute("user");
        //返回login.html页面
        response.sendRedirect(request.getContextPath()+"/login.jsp");
    }

    /**
     * @Author: zhangchunsheng
     * @Date: 2025/1/8 14:15
     * 修改密码
     */
    protected void updatePwd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 获取请求参数
        //原密码
        String userPwd = request.getParameter("user_name");
        //新密码
        String password = request.getParameter("password");
        //从session中获取用户信息
        User user = (User)request.getSession().getAttribute("user");
        if(userPwd.equals(user.getPassword())){
            //原密码正确
            //2. 调用service
            userService.updatePwd(new User(null,user.getUsername(),password,null));
            //3. 响应(修改成功,跳转到登录页面)
            request.setAttribute("pwdSuccess","pwdSuccess");
            //现在我们想让实时显示用户的名字所以就需要做一些修改首先就是转向jsp同时将数据共享到域中
            //所以到底用什么域我们任务request域的url变化后,再回来就无法显示用户名了

            request.getRequestDispatcher("/login.jsp").forward(request,response);
            HttpSession session = request.getSession();
            session.setAttribute("user",user);
        }else{  
            //原密码错误
            request.setAttribute("updatePwdMsg","no");
            request.getRequestDispatcher("/password.jsp").forward(request,response);
        }



    }






    }
